<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.sinodata.bsm.center.purview.FunctionConstants"%>
<%@ page import="com.sinodata.bsm.center.purview.UserFunctionValidate"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>db-config-common</title>
	<%@ include file="../include/common.jsp"%>
	<%@ include file="../include/jbox.jsp"%>
	<%@ include file="../include/table.jsp"%>
	<script type="text/javascript">
	
	function datagrid(){
			var oTable=	$('#depmtList').dataTable();
			oTable.fnPageChange("first",true);
	}
	function cancelAllCheck(){
		$("#dt_check_all").removeAttr("checked");
	}
	//全选和取消全选
    function allClick(isSigle)
    {
    	if(isSigle!=1) //如果是单选就不支持全选
	    	{
	    		var dt_check_all = $('#dt_check_all').attr("checked");
  	    	var allCheckbox = $("input[name='dt_check']");
  	    	if(dt_check_all)
  	    	{
  	    		
  	    		allCheckbox.attr("checked",allCheckbox);  //全选
  	    		$('#depmtList tbody tr').addClass('row_selected');  //背景色全选
  	    	}else
  	    	{
  	    		allCheckbox.attr("checked",false);   //取消全选
  	    		$('#depmtList tbody tr').removeClass('row_selected');  //背景色取消
  	    	}
	    	}
    }
	$(function(){
		$("#ip").keyup(function(event){  
             if(event.keyCode == 13){  
                 search();
             }  
        });  
		
	//加载grid
	var oTable=	$('#depmtList').dataTable({
		"bProcessing": true, 
		"bServerSide": true,
		"sServerMethod": 'POST',
		"sAjaxSource":'${pageContext.request.contextPath}/config/db-config!list.action',
		"bPaginate": true,
		"bSort": true,
		"bFilter":false,
		"bJQueryUI":false,
		"sPaginationType": "full_numbers",
		"sDom":'rt <"bottom"flpi>',
		"iDisplayLength":20,
		"aLengthMenu": [[20, 50, 100], [20, 50,100]],
		"aoColumns":[
		<%if(UserFunctionValidate.sysFunctionValidate(FunctionConstants.SYSTEM_MANAGER)){%>
			{"mDataProp":"checkbox","bSortable": false},
			<%}%>
			{"mDataProp":"op","":"center","bSortable": false},
			{"mDataProp":"ip"},
			{"mDataProp":"instType","bSortable": false},
			{"mDataProp":"instName","bSortable": false},
			{"mDataProp":"port","bSortable": false},
			{"mDataProp":"userName","bSortable": false},
			{"mDataProp":"url","bSortable": false},
			{"mDataProp":"maxConn","bSortable": false},
			{"mDataProp":"id","bVisible":false}
		], 
		 "oLanguage": { 
		 				"sLengthMenu": "每页 _MENU_ 条",
		 		 		"sZeroRecords": "  ",
						"sInfo": "当前从 _START_ 到 _END_ 条,共 _TOTAL_ 条记录", 
						"sInfoEmpty": "没有找到记录",
						"oPaginate":{"sFirst":"首页","sPrevious":"上一页","sNext":"下一页","sLast":"尾页"}	
		},
	    "fnServerParams":function(aoData){
	    	var ip = $('#ip').val();
	    	if(ip!='')
	    	{
	    		aoData.push({'name':'ip','value':ip});
	    	}
	 }});
	$('#depmtList tbody td :checkbox').live('click', function (e) {
		var nTr = $(this).parents('tr')[0]; 
	    	showDetailFlag = true;
		   if ($(nTr).hasClass('row_selected')) {
			  	$(nTr).removeClass('row_selected'); 
			  	cancelAllCheck();
		  }else
		  {
		  		$(nTr).addClass('row_selected'); 
		 }
		 if(!$(this).attr("checked")){
		 	$('#allCheck').attr("checked",false);
		 }
	});
	showDetailFlag = false;
});
function edit(id){
	$.ajax({
  		type : "POST",
  		url : "${pageContext.request.contextPath}/config/db-config!checkDeleted.action", //请求的action
  		data : {id:id}, //传的参数
  		dataType : 'json',
  		success : function(data) { //结果
  			if(data.flag == true){
  				var url ="iframe:config/db-config!input.action?id="+id;
  				var opt = { title: "修改数据库配置",width:570,height:450,buttons: {}};
  				getTopWin(window).openjBox(url,opt,'update-db-config',window);
  			}else{
  				getTopWin(window).getMessageBox().alert("此条配置已经删除","提示");
  				datagrid();
  			}
  		}
  	});
}
function deleteOne(id){
	var submit = function (v, h, f) {
	    if (v == 'ok') {
	    	$.ajax({
	    		type : "POST",
	    		url : "${pageContext.request.contextPath}/config/db-config!delete.action", //请求的action
	    		data : {ids:id}, //传的参数
	    		dataType : 'json',
	    		success : function(data) { //结果
	    			if(data == true){
						window.setTimeout(function () { $.jBox.tip('删除成功。', 'success'); }, 100);
	    			}else{
	    				getTopWin(window).getMessageBox().alert("此条配置已经删除","提示");
	    			}
					datagrid();
	    		}
	    	});
	    }
	    else if (v == 'cancel') {
	    }
	    return true; //close
	};
	getTopWin(window).jBox.confirm("确定要删除数据吗？", "提示", submit);
}
function search(){
	var oTable=	$('#depmtList').dataTable();
	oTable.fnPageChange("first",true);
}
//得到当前选中的
	function fnGetSelected() {   
		var selections = $('#depmtList tr.row_selected');
	    return selections;
    } 
function batchDelete(){
	var rows = fnGetSelected();
	if(rows.length>0){
		var submit = function (v, h, f) {
		    if (v == 'ok') {
		        var mydata = {};
				mydata.Ids = new Array(rows.length);
				var oTable =$('#depmtList').dataTable();
				for(var i = 0 ; i<rows.length;i++){
					var aData = oTable.fnGetData(rows[i]);
					mydata.Ids[i] = aData.id;
				}
				$.ajax({
					type : "POST",
					url : "${pageContext.request.contextPath}/config/db-config!delete.action", //请求的action
					data : jQuery.param(mydata,true), //传的参数
					dataType : 'json',
					success : function(data) { //结果
						if(data == true){
							window.setTimeout(function () { $.jBox.tip('删除成功。', 'success'); }, 100);
		    			}else{
		    				getTopWin(window).getMessageBox().alert("包含已经删除的配置,请重新选择","提示");
		    			}
						cancelAllCheck();
						datagrid();
					}
				});
		        
		    }
		    else if (v == 'cancel') {
		       
		    }

		    return true; //close
		};
		getTopWin(window).jBox.confirm("确定要删除数据吗？", "提示", submit);
	}else{
		getTopWin(window).getMessageBox().alert("请选择一条数据","提示");
	}
}
	function add() {
		var url ="iframe:config/db-config!add.action";
		var opt = { title: "新增数据库配置",width:570,height:450,buttons: {}};
		getTopWin(window).openjBox(url,opt,'add-db-config',window);
		
	}
	function addStyle(){
			$("#ipSpan").css({"display":"none"});
			var oInput = document.getElementById("ip");
			oInput.focus();
		}
		function delStyle(){
			if($("#ip").val() == ''){
				$("#ipSpan").css({"display":""});
			}
		}
		function test(id){
			$.ajax({
		  		type : "POST",
		  		url : "${pageContext.request.contextPath}/config/db-config!checkDeleted.action", //请求的action
		  		data : {id:id}, //传的参数
		  		dataType : 'json',
		  		success : function(data) { //结果
		  			if(data.flag == true){
		  				var url ="iframe:config/config-conn!common.action?id="+id+"&type="+'JDBC';
		  				var opt = { title: "连接测试",width:560,height:230,buttons: {}};
		  				getTopWin(window).openjBox(url,opt,'config-conn',window);
		  			}else{
		  				getTopWin(window).getMessageBox().alert("此条配置已经删除","提示");
		  				datagrid();
		  			}
		  		}
		  	});
		}
	</script>
	
  </head>
  
  <body>
	<h2 class="title-h2" style="margin-top:0; "><span><img src="${pageContext.request.contextPath}/style/default/bsm/images/arrow-down.gif"/></span>数据库配置列表</h2>
	<div class="toolbar" id = "toolbar">
	   <%if(UserFunctionValidate.sysFunctionValidate(FunctionConstants.SYSTEM_MANAGER)){%>
		<a class="page_add" href="javascript:add();">新增</a><a
			class="page_delete" href="javascript:batchDelete();">批量删除</a>
		<%} %>
			<div class="f-r-input" onclick="addStyle();"><input id="ip" name="ip" onfocus="addStyle();" onblur="delStyle();"><span id="ipSpan">搜索IP地址</span></div>
	</div>

	<div>
		<table id="depmtList" width="100%" cellpadding="0" cellspacing="0"
			border="0" class="display">
			<thead>
				<tr>
				<%if(UserFunctionValidate.sysFunctionValidate(FunctionConstants.SYSTEM_MANAGER)){%>
					<th width="2%">&nbsp;&nbsp;<input type='checkbox' name='dt_check_all'
					id="dt_check_all" onclick="allClick()" /></th>
						<%} %>
					<th width="10%">操作</th>
					<th width="15%">IP</th>
					<th width="10%">类型</th>
					<th width="10%">数据库名</th>
					<th width="10%">端口</th>
					<th width="10%">帐号</th>
					<th >连接串</th>
					<th width="10%">最大连接数</th>
				</tr>
			</thead>
		</table>
	</div>
</body>
</html>
